package edu.tyut.game.controller.poem;

import edu.tyut.game.config.SocketConfig;
import edu.tyut.game.model.PoemPoet;
import edu.tyut.game.model.Result;
import edu.tyut.game.service.PoemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/poem")
public class PoemController {
    private PoemService poemService;

    @Autowired
    public void setPoemService(PoemService poemService) {
        this.poemService = poemService;
    }

    // 诗词推荐
    @GetMapping("/getPoemOrderByClickCount")
    public Result<List<PoemPoet>> getPoemOrderByClickCount() {
        List<PoemPoet> poemPoets = poemService.getPoemOrderByClickCount();
        return Result.success("获取数据成功", poemPoets);
    }

    @PostMapping("/getDynasties")
    public Result<List<String>> getDynasties() {
        List<String> dynasties = poemService.getDynasties();
        return Result.success("获取数据成功", dynasties);
    }

    @GetMapping("/getPoemListByDynasty/{dynasty}")
    public Result<List<PoemPoet>> getPoemListByDynasty(@PathVariable("dynasty") String dynasty) {
        List<PoemPoet> poemListByDynasty = poemService.getPoemListByDynasty(dynasty);
        return Result.success("获取数据成功", poemListByDynasty);
    }

    @GetMapping("/getPoemListBySearch/{content}")
    public Result<List<PoemPoet>> getPoemListBySearch(@PathVariable("content") String content) {
        List<PoemPoet> poemListBySearch = poemService.getPoemListBySearch(content);
        return Result.success("获取数据成功", poemListBySearch);
    }

    private SocketConfig socketConfig;
    @Autowired
    public void setSocketConfig(SocketConfig socketConfig) {
        this.socketConfig = socketConfig;
    }
    /*
        flyGame       uid 局数 输赢(0,1) 时间戳
        PoemRecommend uid poemId poetId 1 1684724518118
     */
    @PostMapping("/poemRecommend")
    public Result<String> poemRecommend(@RequestBody String ids) {
        String[] strings = ids.replace("\"", "").split("\\s+");
        if (strings.length == 3){
            log.error("PoemRecommend,{},{},{},{},{}", strings[0], strings[1], strings[2], 1, System.currentTimeMillis());
            // socket写出
            socketConfig.writeInfo(String.format("PoemRecommend,%s,%s,%s,%s,%s", strings[0], strings[1], strings[2], 1, System.currentTimeMillis()));
        }

        return Result.success("埋点成功", "埋点成功");
    }


}
